﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_ChuyenBan : Form
    {
        public Frm_ChuyenBan()
        {
            InitializeComponent();
        }
        string BDi = "";
        string BDen = "";
        private void Frm_ChuyenBan_Load(object sender, EventArgs e)
        {
            Load_LV_BDi();
            Load_LV_BDen();
        }
        public void Load_LV_BDi()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MA_BDi();
            load_BDi(tb);
        }
        public void load_BDi(DataTable tb)
        {
            LV_BDi.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "1")
                    item.ImageIndex = 1;
                else
                {
                    if (tb.Rows[i][2].ToString() == "6")
                        item.ImageIndex = 2;
                }
                LV_BDi.Items.Add(item);
            }
        }

        public void Load_LV_BDen()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MA_BDen();
            load_BDen(tb);
        }
        public void load_BDen(DataTable tb)
        {
            LV_BDen.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "5")
                    item.ImageIndex = 1;
                else
                {
                    if (tb.Rows[i][2].ToString() == "0")
                        item.ImageIndex = 2;
                }
                LV_BDen.Items.Add(item);
            }
        }

        private void LV_BDi_MouseClick(object sender, MouseEventArgs e)
        {
            BDi = LV_BDi.FocusedItem.SubItems[0].Text;
            Lbl_BDi.Text = BDi;
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=6");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=6 WHERE MaBan='{0}'", BDi);
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
            Load_LV_BDi();
            Load_LV_BDen();
        }

        private void LV_BDen_MouseClick(object sender, MouseEventArgs e)
        {
            BDen = LV_BDen.FocusedItem.SubItems[0].Text;
            Lbl_BDen.Text = BDen;
            string st = string.Format("UPDATE Ban SET TinhTrang=5 WHERE TinhTrang=2");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=2 WHERE MaBan='{0}'", BDen);
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
            Load_LV_BDen();
        }

        private void Frm_ChuyenBan_FormClosed(object sender, FormClosedEventArgs e)
        {
            string st = string.Format("UPDATE Ban SET TinhTrang=1 WHERE TinhTrang=6");
            SqlCommand com = new SqlCommand(st, Coso.con);
            com.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=5 WHERE TinhTrang=2");
            SqlCommand comd = new SqlCommand(str, Coso.con);
            comd.ExecuteNonQuery();
        }

        private void Btn_ChuyenBan_Click(object sender, EventArgs e)
        {
            string st = string.Format("UPDATE Ban SET TinhTrang=0 WHERE TinhTrang=6");
            SqlCommand com = new SqlCommand(st, Coso.con);
            com.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=1, GhepBan='{0}' WHERE TinhTrang=2",Lbl_BDen.Text);
            SqlCommand comd = new SqlCommand(str, Coso.con);
            comd.ExecuteNonQuery();
            string str_bg = string.Format("UPDATE Ban SET GhepBan='{0}' WHERE GhepBan='{1}'", Lbl_BDen.Text,Lbl_BDi.Text);
            SqlCommand com_bg = new SqlCommand(str_bg, Coso.con);
            com_bg.ExecuteNonQuery();
            string strhd = string.Format("UPDATE HoaDon SET MaBan='{0}'",Lbl_BDen.Text);
            SqlCommand com_hd = new SqlCommand(strhd, Coso.con);
            com_hd.ExecuteNonQuery();
            string strcthd = string.Format("UPDATE ChiTietHoaDon SET MaBan='{0}' WHERE MaBan='{1}'", Lbl_BDen.Text,Lbl_BDi.Text);
            SqlCommand com_cthd = new SqlCommand(strcthd, Coso.con);
            com_cthd.ExecuteNonQuery();
            Load_LV_BDi();
            Load_LV_BDen();
        }
    }
}
